package com.ibm.plugins.parse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;  
import org.jdom2.Document;  
import org.jdom2.Element;  
import org.jdom2.input.SAXBuilder;  
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.xpath.XPath; 
@SuppressWarnings("deprecation")
public class restServiceParse {  
 @SuppressWarnings({"unchecked" })
public static void main(String[] args) throws Exception {
  SAXBuilder sb = new SAXBuilder();
  Document doc = sb.build("repo.xml");
  Element root = doc.getRootElement();
  List list = XPath.selectNodes(root, "/report/group//package/class[contains(@name ,'RestService')]");
  List allList = XPath.selectNodes(root, "/report/group//package/*");
  System.out.println(allList.size());
  System.out.println(list.size());
  for(int i = 0;i<list.size();i++){
	  Element element = (Element)list.get(i);
	  if(allList.contains(element)){
		  allList.remove(element);
	  }
  }
  System.out.println(allList.size());
  for(int j = 0;j<allList.size();j++){
	  Element element = (Element)allList.get(j);
	  Element parentElement = element.getParentElement();
	  parentElement.removeContent(element);
	  while(0 == parentElement.getChildren().size()){
		  Element temp = parentElement;
		  parentElement=temp.getParentElement();
		  parentElement.removeContent(temp);
	  }
  }
  
  Format format = Format.getPrettyFormat();
  format.setEncoding("utf-8");
  format.setIndent("  ");
  XMLOutputter XMLOut = new XMLOutputter(format);
  try {
   XMLOut.output(doc, new FileOutputStream("newReport.xml"));
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }
 
}